/*******************************************************************************
 * Copyright (c) 2008 Sylvain Sicard.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Sylvain Sicard - initial API and implementation
 ******************************************************************************/
package fr.jade.base;

import java.util.Collection;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.BindingController;
import org.objectweb.fractal.api.control.IllegalBindingException;
import org.objectweb.fractal.api.control.IllegalLifeCycleException;

import fr.jade.fraclite.api.control.GenericAttributeController;
import fr.jade.fraclite.api.control.NoSuchAttributeException;
import fr.jade.util.FractalNavigator;

public abstract class JadeBaseComponent extends JadeBaseObject implements BindingController, GenericAttributeController {

	protected Component self = null;
	protected String selfName = null;
	protected String loggerPrefix = null;
  
  public static final String logAttName = "log"; // legal values are "true" or "false"

	// Implem of Binding Controller
	
	public void bindFc(String itfName, Object itfValue) throws NoSuchInterfaceException, IllegalBindingException, IllegalLifeCycleException {
		if (itfName.equals("component")){
			self = (Component)itfValue;
		} else {
		  throw new NoSuchInterfaceException(itfName);
		}
	}
	
	public String[] listFc() {
		return new String[]{};
	}
	
	public Object lookupFc(String itfName) throws NoSuchInterfaceException {
		throw new NoSuchInterfaceException(itfName);
	}

	public void unbindFc(String itfName) throws NoSuchInterfaceException, IllegalBindingException, IllegalLifeCycleException {
		throw new NoSuchInterfaceException(itfName);
	}

	// Implem of Attribute Controller
	
	public String getAttribute(String name) throws NoSuchAttributeException {
		if(name.equals(logAttName)){
			return Boolean.toString(log);
		}
		throw new NoSuchAttributeException(name);
	}

	public void setAttribute(String name, String value) throws NoSuchAttributeException {
    if(name.equals(logAttName)){
      assert value != null && (value.equals("true") || value.equals("false"));
      log = Boolean.parseBoolean(value);
    }else{
      throw new NoSuchAttributeException(name);
    }
  }
	
	public String[] listFcAtt() {
		return new String[] {logAttName};
	}

	protected void log(String msg){
		if(log){
			if(selfName == null){
				try{
					selfName = FractalNavigator.getName(self);
					
				}catch(Exception e){
					selfName =  this.getClass().getName() ;
				}
				loggerPrefix = "[" + selfName + "] ";
			}
			
			super.log(loggerPrefix + msg);
		}
	}
	protected void log(Exception e){
		log("");
		super.log(e);
	}
	
	protected String[] concat(String[] t1, String[] t2){
	 String[] r = new String[t1.length + t2.length];
	 int i=0;
	 for(String s : t1) r[i++]=s;
	 for(String s : t2) r[i++]=s;
	 return r;
	}
	
	protected String[] concat(Collection<String> l, String[] t){
	   String[] r = new String[l.size() + t.length];
	   int i=0;
	   for(String s : l) r[i++]=s;
	   for(String s : t) r[i++]=s;
	   return r;
	  }
}
